<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
	<title>
LuaJava - ferramenta de scripts para Java</title>    <link rel="stylesheet" href="http://www.keplerproject.org/doc.css" type="text/css"/>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>

<body>

<div id="container">

<div id="product">
	<div id="product_logo"><a href="http://www.keplerproject.org"><img alt="LuaJava" src="luajava_128.png"/></a></div>
	<div id="product_name"><big><b>LuaJava</b></big></div>
	<div id="product_description">Ferramenta de scripts para Java</div>
</div> <!-- id="product" -->

<div id="main">
<div id="navigation">
	
<h1>LuaJava</h1>
	<ul>
		<li><a href="index.html">In&iacute;cio</a>
			<ul>
				<li><a href="index.html#overview">Vis&atilde;o geral</a></li>
				<li><a href="index.html#status">Status</a></li>
				<li><a href="index.html#dependencies">Depend&ecirc;ncias</a></li>
				<li><a href="index.html#download">Download</a></li>
				<li><a href="index.html#credits">Cr&eacute;ditos</a></li>
				<li><a href="index.html#contact">Fale conosco</a></li>
			</ul>
		</li>
		<li><a href="manual.html">Manual</a>
			<ul>
				<li><a href="manual.html#compile">Compila&ccedil;&atilde;o</a></li>
				<li><a href="manual.html#install">Instala&ccedil;&atilde;o</a></li>
				<li><a href="manual.html#console">Console Java</a></li>
				<li><a href="manual.html#luareference">Refer&ecirc;ncia de Lua</a></li>
				<li><a href="manual.html#javareference">Refer&ecirc;ncia de Java</a></li>
			</ul>
		</li>
		<li><strong>Exemplos</strong></li>
		<li><a href="history.html">Hist&oacute;rico</a></li>
    <li><a href="http://luaforge.net/projects/luajava/">Projeto</a>
      <ul>
          <li><a href="http://luaforge.net/tracker/?group_id=10">Bug Tracker</a></li>
          <li><a href="http://luaforge.net/scm/?group_id=10">CVS</a></li>
      </ul>
    </li>
		<li><a href="license.html">Licen&ccedil;a</a></li>
	</ul>
</div> <!-- id="navigation" -->

<div id="content">

<h2>Exemplos</h2>

<h4>"Ol&aacute; Mundo" usando LuaJava</h4>

<p>Este exemplo &eacute; um aplicativo simples "Ol&aacute; Mundo" que usa o LuaJava para imprimir de Lua e Java.</p>

<p><code>Hello.java</code></p>
<pre class="example">public class Hello
{
  public static void main(String[] args)
  {
    LuaState L = LuaStateFactory.newLuaState();
    L.openLibs();
    
    L.LdoFile(&quot;hello.lua&quot;);
    
    System.out.println(&quot;Ol&aacute; Mundo de Java!&quot;);
  }
}
</pre>

<p><code>hello.lua</code></p>

<pre class="example">print(&quot;Ol&aacute; Mundo de Lua!&quot;)
</pre>

<h4>Acesso a um banco de dados</h4>

<p>Este exemplo usa Java JDBC para acessar um banco de dados a partir de Lua.</p>

<p><code>TestJDBC.java</code></p>
<pre class="example">public class TestJDBC
{
  
  public static void main(String[] args) throws Exception
  {
    // obt&eacute;m uma java.sql.Connection e cria um Statement

    Class.forName(&quot;org.hsqldb.jdbcDriver&quot;);
    Connection con = DriverManager.getConnection(
	                    &quot;jdbc:hsqldb:hsql://localhost:9002&quot;,
	                    &quot;sa&quot;, &quot;&quot;);
    Statement st = con.createStatement();
    
    try
    {
      st.execute(&quot;DROP TABLE luatest&quot;);
    }
    catch (Exception ignore) {}
    
    st.execute([[CREATE TABLE luatest 
               (id int not null primary key, 
               str varchar, number double)]]);
    
    for(int i = 0 ; i &lt; 10 ; i++)
    {
      st.executeUpdate(
          &quot;INSERT INTO luatest (id, str, number) values(&quot; + 
          i + &quot;, '&quot; + 2*i + &quot;', &quot; + 
          System.currentTimeMillis() + &quot;)&quot;);
    }
    
    // cria um estado Lua e registra as bibliotecas b&aacute;sicas
    
    LuaState L = LuaStateFactory.newLuaState();
    L.openLibs();
    
    // registra a instru&ccedil;&atilde;o no 'st' global
    
    L.pushObjectValue(st);
    L.setGlobal(&quot;st&quot;);
    
    // executa o arquivo testJDBC.lua
    
    int err = L.LdoFile(&quot;testJDBC.lua&quot;);

    L.close();
    st.close();
    con.close();
  }
}
</pre>


<p><code>testJDBC.lua</code></p>
<pre class="example">do
-- testa se o Statement &eacute; nil
if st == nil then
    print(&quot;Erro. Objeto st &eacute; nil&quot;)
    return
end

local st = st
_G.st = nil

function createSQLProxy(t)

    local tableName = t
    
    local function i (t,k)
    
        local id = tonumber(k)
        if not id then
            return nil
        end
        
        local function mi (t,k)
        
            local sql = &quot;select &quot;..k..&quot; from &quot;..
                        tableName..&quot; where id=&quot;..id
            local rs = st:executeQuery(sql)
            if not rs:next() then
                rs:close()
                return nil
            end
            
            local res = rs:getString(1)
            rs:close()
            
            return res
        end
        
        local res = {}
        setmetatable(res, {__index = mi})
        return res
    end
    
    local res = {}
    setmetatable(res, {__index = mi})
    return res
end

end


proxy = createSQLProxy(&quot;luatest&quot;)

print(proxy[1].str)
print(proxy[1].number)

</pre>

</div> <!-- id="content" -->

</div> <!-- id="main" -->


<div id="about">
	<p><a href="http://validator.w3.org/check?uri=referer">
    <img src="http://www.w3.org/Icons/valid-xhtml10" alt="Valid XHTML 1.0!" height="31" width="88" /></a></p>
      <p><small>$Id: examples.html,v 1.5 2007-01-23 22:37:28 thiago Exp $</small></p>
</div> <!-- id="about" -->

</div> <!-- id="container" -->

</body>
</html> 
